home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WDBASE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-14  |  5.6 KB  |  223 lines

  1. #include <WDBase.h>
  2. #include <WFile.h>
  3. #pragma hdrstop
  4.  
  5. // copyright (c) 1993 Paul Wheaton
  6.  
  7. class DBaseSystem
  8.   {
  9.     public:
  10.       C4CODE CodeBaseGlobalData;
  11.       DBaseSystem()
  12.         {
  13.           d4init(&CodeBaseGlobalData);
  14.           CodeBaseGlobalData.read_lock=0;
  15.         }
  16.       ~DBaseSystem()
  17.         {
  18.           d4close_all(&CodeBaseGlobalData);
  19.           e4exit(&CodeBaseGlobalData);
  20.         }
  21.   } DBaseSys;
  22.  
  23. void DBaseAutoOpen(Bool OnOff)
  24.   {
  25.     DBaseSys.CodeBaseGlobalData.auto_open=OnOff;
  26.   }
  27.  
  28. DBase::DBase(const char* FileName)
  29.   {
  30.     D=d4open(&(DBaseSys.CodeBaseGlobalData),(char*)FileName);
  31.     if (D==NULL) FatalError("dbase null "+String(FileName));
  32.     e4exit_test(&(DBaseSys.CodeBaseGlobalData));
  33.     Closed=False;
  34.     int I=d4num_fields(D);
  35.     while(I>0)
  36.       {
  37.         V[I]=d4field_j(D,I);;
  38.         I--;
  39.       }
  40.   }
  41.  
  42. void DBase::Close()
  43.   {
  44.     if (!Closed)
  45.       {
  46.         d4close(D);
  47.         Closed=True;
  48.       }
  49.   }
  50.  
  51. void DBase::SetIndexTag(const char* TagName)
  52.   {
  53.     T4TAG* tag=d4tag(D,(char*)TagName);
  54.     if (tag==NULL) FatalError("set tag");
  55.     d4tag_select(D,tag);
  56.   }
  57.  
  58. T4TAG* DBase::Tag(const char* TagName)
  59.   {
  60.     if (TagName==NULL) return d4tag_default(D);
  61.     else return d4tag(D,(char*)TagName);
  62.   }
  63.  
  64. String40 DateToDbase(const Date& D)
  65.   {
  66.     String40 S=Str(D.Year())+D.MonthStr2()+D.DayStr();
  67.     return S;
  68.   }
  69.  
  70. Date DbaseToDate(const char* CS)
  71.   {
  72.     String40 S(CS);
  73.     Date D(atoi(S.Before(4)),atoi(S(4,2)),atoi(S(6,2)));
  74.     return D;
  75.   }
  76.  
  77. static void MallocAndCopy(char*& A,const char* B)
  78.   {
  79.     A=(char*)malloc(strlen(B)+1);
  80.     strcpy(A,B);
  81.   }
  82.  
  83. void CreateDBaseFile::DefineField(const char* FieldName,char Type,int Width,int Decimals)
  84.   {
  85.     F4FIELD_INFO F;
  86.     MallocAndCopy(F.name,FieldName);
  87.     F.type=Type;
  88.     F.len=Width;
  89.     F.dec=Decimals;
  90.     FV+=F;
  91.   }
  92.  
  93. const char* NotDeleted=".NOT. DELETED()";
  94.  
  95. static void AddTag(CDBTVector& TV, const char* TN, const char* Key,
  96.     const char* Filter, int Unique, int D)
  97.   {
  98.     T4TAG_INFO T;
  99.     MallocAndCopy(T.name,TN);
  100.     MallocAndCopy(T.expression,Key);
  101.     if (Filter==NULL) T.filter=NULL;
  102.     else MallocAndCopy(T.filter,Filter);
  103.     T.unique=Unique;
  104.     T.descending=D;
  105.     TV+=T;
  106.   }
  107.  
  108. void CreateDBaseFile::DefineTag(const char* TagName, const char* KeyExpression,
  109.           const char* FilterExpression, int Unique, int Descending)
  110.   {
  111.     AddTag(TV,TagName,KeyExpression,FilterExpression,Unique,Descending);
  112.   }
  113.  
  114. void CreateDBaseFile::Done()
  115.   {
  116.     if (Name.Length()>0)
  117.       {
  118.         DeleteFile(Name);
  119.         F4FIELD_INFO F={NULL,0,0,0};
  120.         FV+=F;
  121.         T4TAG_INFO* TA;
  122.         if (TV.Size()==0) TA=NULL;
  123.         else
  124.           {
  125.             T4TAG_INFO T={NULL,NULL,NULL,0,0};
  126.             TV+=T;
  127.             TA=&TV[0];
  128.           }
  129.         D4DATA* D=d4create(&(DBaseSys.CodeBaseGlobalData),
  130.             (char*)(const char*)Name,&FV[0],TA);
  131.         d4close(D);
  132.         FV.Pop();
  133.         TV.Pop();
  134.         int I;
  135.         For(I,FV.Size()) free(FV[I].name);
  136.         For(I,TV.Size())
  137.           {
  138.             free(TV[I].name);
  139.             free(TV[I].expression);
  140.             char* F=TV[I].filter;
  141.             if (F!=NULL) free(F);
  142.           }
  143.         Name.Clip(0);
  144.       }
  145.   }
  146.  
  147. /*
  148. void CreateDBaseIndex(DBase& DB,const char* IndexFileName,const char* TagName,
  149.     const char* KeyExpression,const char* FilterExpression,
  150.     int Unique,int Descending)
  151.   {
  152.     CDBTVector TV;
  153.     T4TAG_INFO T;
  154.     MallocAndCopy(T.name,TagName);
  155.     MallocAndCopy(T.expression,KeyExpression);
  156.     if (FilterExpression==NULL) T.filter=NULL;
  157.     else MallocAndCopy(T.filter,FilterExpression);
  158.     T.unique=Unique;
  159.     T.descending=Descending;
  160.     TV+=T;
  161.     String120 Name=IndexFileName;
  162.     DeleteFile(Name);
  163.     T4TAG_INFO NullT={NULL,NULL,NULL,0,0};
  164.     TV+=NullT;
  165.     I4INDEX* X=i4create(DB,(char*)(const char*)Name,&TV[0]);
  166.     if (X==NULL) FatalError("dbase index create");
  167.     if (i4reindex(X)!=0) FatalError("dbase index create 2");
  168.     DB.SetIndexTag(TagName);
  169.     TV.Pop();
  170.     free(TV[0].name);
  171.     free(TV[0].expression);
  172.     char* F=TV[0].filter;
  173.     if (F!=NULL) free(F);
  174.   }
  175. */
  176.  
  177. void CreateADBaseIndex(DBase& DB,const char* IndexFileName,const char* TagName,
  178.     const char* KeyExpression,const char* FilterExpression,
  179.     int Unique,int Descending)
  180.   {
  181.     CreateDBaseIndex DBX(DB,IndexFileName);
  182.     DBX.DefineTag(TagName,KeyExpression,FilterExpression,Unique,Descending);
  183.   }
  184.  
  185. CreateDBaseIndex::CreateDBaseIndex(DBase& D,const char* IndexFileName)
  186.   {
  187.     DB=&D;
  188.     if (IndexFileName!=NULL) Name=IndexFileName;
  189.     Finished=False;
  190.   }
  191.  
  192. void CreateDBaseIndex::DefineTag(const char* TagName, const char* KeyExpression,
  193.     const char* FilterExpression, int Unique, int Descending)
  194.   {
  195.     AddTag(TV,TagName,KeyExpression,FilterExpression,Unique,Descending);
  196.   }
  197.  
  198. void CreateDBaseIndex::Done()
  199.   {
  200.     if (!Finished)
  201.       {
  202.         DeleteFile(Name);
  203.         T4TAG_INFO NullT={NULL,NULL,NULL,0,0};
  204.         TV+=NullT;
  205.         char* N=NULL;
  206.         if (Name.Length()>0) N=(char*)(const char*)Name;
  207.         I4INDEX* X=i4create(*DB,N,&TV[0]);
  208.         if (X==NULL) FatalError("dbase index create");
  209.         if (i4reindex(X)!=0) FatalError("dbase index create 2");
  210.         DB->SetIndexTag(TV[0].name);
  211.         TV.Pop();
  212.         int I;
  213.         For(I,TV.Size())
  214.           {
  215.             free(TV[I].name);
  216.             free(TV[I].expression);
  217.             char* F=TV[I].filter;
  218.             if (F!=NULL) free(F);
  219.           }
  220.         Finished=True;
  221.       }
  222.   }
  223.